﻿

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win.UltraWinGrid;
using WHC.OrderWater.Commons;
using WHC.Pager.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using Infragistics.Win;
using Maticsoft.DBUtility;

namespace CKNetFrameWork
{
    public partial class Frm_PayIn : DockContent
    {

        private TrainingDB.BLL.CK_StudentClassInfo m_bllCK_StudentClassInfo = new TrainingDB.BLL.CK_StudentClassInfo();
        private TrainingDB.BLL.CK_PayInDetailOtherTab m_bllCK_PayInDetailOtherTab = new TrainingDB.BLL.CK_PayInDetailOtherTab();
        private TrainingDB.BLL.CK_OtherFeeItemTab m_bllCK_OtherFeeItemTab = new TrainingDB.BLL.CK_OtherFeeItemTab();

        
        private TrainingDB.BLL.CK_PayInDetailClassTab m_bllCK_PayInDetailClassTab = new TrainingDB.BLL.CK_PayInDetailClassTab();
        private TrainingDB.BLL.CK_StudentInfo m_bllCK_StudentInfo = new TrainingDB.BLL.CK_StudentInfo();
        private TrainingDB.BLL.CK_PayInTab m_bllCK_PayInTab = new TrainingDB.BLL.CK_PayInTab();
        private TrainingDB.BLL.CK_SchoolTab m_bllCK_SchoolTab = new TrainingDB.BLL.CK_SchoolTab();
        private TrainingDB.BLL.CK_PayWayTab m_bllCK_PayWayTab = new TrainingDB.BLL.CK_PayWayTab();
        private TrainingDB.BLL.CK_BankAccountTab m_bllCK_BankAccountTab = new TrainingDB.BLL.CK_BankAccountTab();
        private TrainingDB.BLL.T_ACL_User m_bllT_ACL_User = new TrainingDB.BLL.T_ACL_User();
        //private TrainingDB.BLL.CK_PayInTabDetail m_bllCK_PayInTabDetail = new TrainingDB.BLL.CK_PayInTabDetail();
        private TrainingDB.BLL.CK_ConsultWayTab m_bllCK_ConsultWayTab = new TrainingDB.BLL.CK_ConsultWayTab();
        private TrainingDB.BLL.CK_ClassTab m_bllCK_ClassTab = new TrainingDB.BLL.CK_ClassTab();
        private int m_nStudentReferID = -1;
        private int m_nDelIndex = -1;
        private int m_nDelIndex2 = -1;
        private int m_nDelIndex3 = -1;
        private bool m_bInsertFlag = false;
        private bool m_bInsertFlag2 = false;
        private bool m_bInsertFlag3 = false;
        private Dictionary<string, string> m_dicColName2AliasName = new Dictionary<string, string>();
        private Dictionary<string, string> m_dicColAliasName2Name = new Dictionary<string, string>();
        private string m_filter = "";

        public Frm_PayIn()
        {
            InitializeComponent();
        }


        private void ShowAllRecordInDataGridView(string strWhere)
        {
            BindData(strWhere);
        }

        private void SearchResult()
        {
            this.pager1.CurrentPageIndex = 1;
            m_filter = "";
            int nIndex = this.ultraGrid1.ActiveRow.Index;
            List<object> lstO = this.ultraGrid1.DisplayLayout.Bands[0].ColumnFilters.All.ToList();

            string strColumnName;
            string strColumnValue;
            SearchCondition condition = new SearchCondition();
            foreach (object o in lstO)
            {
                ColumnFilter cf = (ColumnFilter)o;
                strColumnName = cf.Column.Key;
                strColumnValue = cf.FilterConditions.Count > 0 ? ((FilterCondition)cf.FilterConditions.All[0]).CompareValue.ToString() : "";
                if (strColumnValue != "")
                {
                    condition = condition.AddCondition(m_dicColAliasName2Name[strColumnName], strColumnValue, SqlOperator.Equal);
                }
            }

            m_filter = condition.BuildConditionSql();
            m_filter = m_filter.Replace("Where", "");
            ShowAllRecordInDataGridView(m_filter);
        }
        private void AddNewRow()
        {
            if (Frm_Main.CheckPerm(this.Text, "增加"))
            {
                UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
            }
            else
            {
                MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        private void AddNewRowultraGrid2()
        {
            if (Frm_Main.CheckPerm(this.Text, "增加"))
            {
                UltraGridRow row = this.ultraGrid2.DisplayLayout.Bands[0].AddNew();
            }
            else
            {
                MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        private void AddNewRowultraGrid3()
        {
            if (Frm_Main.CheckPerm(this.Text, "增加"))
            {
                UltraGridRow row = this.ultraGrid3.DisplayLayout.Bands[0].AddNew();
            }
            else
            {
                MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        private void ultraGrid1_KeyDown_1(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SearchResult();
            }
          
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    AddNewRow();
                    //UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        string strTemp = this.ultraGrid1.ActiveCell.Column.Header.Caption;

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }

        }
        private bool AddGoodsDetail(int nRowIndex)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];

                TrainingDB.Model.CK_PayInTab modelCK_PayInTab = new TrainingDB.Model.CK_PayInTab();
                modelCK_PayInTab.CK_SchoolTabID = 1;
                //modelCK_PayInTab.CK_SchoolTabID = Convert.ToInt32(CurrentUGR.Cells["校区名称"].Value.ToString(), 10);
                modelCK_PayInTab.CK_OperUserID = Frm_Main.m_nLoginUserId;
                modelCK_PayInTab.CK_StudentInfoID = Convert.ToInt32(CurrentUGR.Cells["学员姓名"].Value.ToString());
                modelCK_PayInTab.CK_PayWayTabID = Convert.ToInt32(CurrentUGR.Cells["付款方式"].Value.ToString());
                modelCK_PayInTab.CK_BankAccountTabID = Convert.ToInt32(CurrentUGR.Cells["银行账户"].Value.ToString());

                modelCK_PayInTab.payInDate = (DateTime)CurrentUGR.Cells["付款日期"].Value;
                modelCK_PayInTab.tuitionSum = Convert.ToDecimal(CurrentUGR.Cells["学费"].Value.ToString());
                modelCK_PayInTab.bookFeeSum = Convert.ToDecimal(CurrentUGR.Cells["教材费"].Value.ToString());
                modelCK_PayInTab.otherFeeSum = Convert.ToDecimal(CurrentUGR.Cells["其他费"].Value.ToString());
                modelCK_PayInTab.sumFee = Convert.ToDecimal(CurrentUGR.Cells["合计"].Value.ToString());
                modelCK_PayInTab.payInMemo = CurrentUGR.Cells["备注"].Value.ToString();
                modelCK_PayInTab.inTime = System.DateTime.Now;

            


                int nId = m_bllCK_PayInTab.Add(modelCK_PayInTab);
                this.pager1.CurrentPageIndex = 1;
                ShowAllRecordInDataGridView("CK_PayInTabid = '" + nId.ToString() + "'");
                //          MessageBox.Show("添加教室成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        private void InitNewAddRowValue(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid1.Rows[nIndex];

            //CurrentUGR.Cells["校区名称"].Value = 1;
            CurrentUGR.Cells["学员姓名"].Value = 1;
            CurrentUGR.Cells["付款方式"].Value = 1;
            CurrentUGR.Cells["银行账户"].Value = 1;
            CurrentUGR.Cells["付款日期"].Value = System.DateTime.Now.Date;

            CurrentUGR.Cells["学费"].Value = 0;
            CurrentUGR.Cells["教材费"].Value = 0;
            CurrentUGR.Cells["其他费"].Value = 0;
            CurrentUGR.Cells["合计"].Value = 0;
            CurrentUGR.Cells["备注"].Value = "";
          

            if (m_bInsertFlag)
            {
                ultraGrid1.Rows[nIndex].Cells[0].Activation = Activation.ActivateOnly;
                m_bInsertFlag = false;
                AddGoodsDetail(nIndex);

            }

        }

        private void ultraGrid1_AfterRowInsert_1(object sender, RowEventArgs e)
        {
            ultraGrid1.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid1.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid1.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag = true;
            InitNewAddRowValue(e.Row.Index);
        }

        private bool ModifyGoodsDetail(int nRowIndex)
        {

            try
            {
                if (!Frm_Main.CheckPerm(this.Text, "修改"))
                {
                    MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                TrainingDB.Model.CK_PayInTab modelCK_PayInTab = m_bllCK_PayInTab.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

                //modelCK_PayInTab.CK_SchoolTabID = Convert.ToInt32(CurrentUGR.Cells["校区名称"].Value.ToString(), 10);
                modelCK_PayInTab.CK_OperUserID = Frm_Main.m_nLoginUserId;
                //modelCK_PayInTab.CK_StudentInfoID = Convert.ToInt32(CurrentUGR.Cells["学员姓名"].Value.ToString());
                //modelCK_PayInTab.CK_PayWayTabID = Convert.ToInt32(CurrentUGR.Cells["付款方式"].Value.ToString());
                //modelCK_PayInTab.CK_BankAccountTabID = Convert.ToInt32(CurrentUGR.Cells["银行账户"].Value.ToString());

                modelCK_PayInTab.payInDate = (DateTime)CurrentUGR.Cells["付款日期"].Value;
                modelCK_PayInTab.tuitionSum = Convert.ToDecimal(CurrentUGR.Cells["学费"].Value.ToString());
                modelCK_PayInTab.bookFeeSum = Convert.ToDecimal(CurrentUGR.Cells["教材费"].Value.ToString());
                modelCK_PayInTab.otherFeeSum = Convert.ToDecimal(CurrentUGR.Cells["其他费"].Value.ToString());
                modelCK_PayInTab.sumFee = Convert.ToDecimal(CurrentUGR.Cells["合计"].Value.ToString());
                modelCK_PayInTab.payInMemo = CurrentUGR.Cells["备注"].Value.ToString();
                modelCK_PayInTab.inTime = System.DateTime.Now;

                //string strBGH = CurrentUGR.Cells["校区名称"].Value.ToString();
                //if (ClassInputValid.IsNum(strBGH))
                //{
                //    modelCK_PayInTab.CK_SchoolTabID = Convert.ToInt32(CurrentUGR.Cells["校区名称"].Value.ToString(), 10);
                //}
                //else
                //{
                //    DataSet ds = m_bllCK_SchoolTab.GetList("CK_SchoolTabName = '" + strBGH + "'");
                //    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //    {
                //        modelCK_PayInTab.CK_SchoolTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["CK_SchoolTabID"].ToString(), 10);
                //    }
                //}

                string strBGH = CurrentUGR.Cells["学员姓名"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelCK_PayInTab.CK_StudentInfoID = Convert.ToInt32(CurrentUGR.Cells["学员姓名"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllCK_StudentInfo.GetList("stdtName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelCK_PayInTab.CK_StudentInfoID = Convert.ToInt32(ds.Tables[0].Rows[0]["CK_StudentInfoID"].ToString(), 10);
                    }
                }

                strBGH = CurrentUGR.Cells["付款方式"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelCK_PayInTab.CK_PayWayTabID = Convert.ToInt32(CurrentUGR.Cells["付款方式"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllCK_PayWayTab.GetList("CK_PayWayName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelCK_PayInTab.CK_PayWayTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["CK_PayWayTabID"].ToString(), 10);
                    }
                }

                strBGH = CurrentUGR.Cells["银行账户"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelCK_PayInTab.CK_BankAccountTabID = Convert.ToInt32(CurrentUGR.Cells["银行账户"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllCK_BankAccountTab.GetList("bankName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelCK_PayInTab.CK_BankAccountTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["CK_BankAccountTabID"].ToString(), 10);
                    }
                }

                m_bllCK_PayInTab.Update(modelCK_PayInTab);

                // MessageBox.Show("修改教室成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid1_AfterCellActivate_1(object sender, EventArgs e)
        {
            try
            {
                int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
                if (iIndex < 0)
                    return;
                m_nDelIndex = ultraGrid1.ActiveRow.Index;
                UltraGridRow CurrentUGR = ultraGrid1.Rows[m_nDelIndex];
                m_nStudentReferID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells["CK_PayInTabID"].Value.ToString());

                ShowAllMXRecordInDataGridView("CK_PayInTabID = '" + m_nStudentReferID + "'");
                ShowAllMXRecordInDataGridViewOtherFee("CK_PayInTabID = '" + m_nStudentReferID + "'");

            }
            catch (Exception ex)
            {

            }

        }

        private void ultraGrid1_BeforeRowUpdate_1(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                if (m_bInsertFlag)
                {
                    ultraGrid1.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;
                    if (!AddGoodsDetail(e.Row.Index))
                        e.Cancel = true;
                }
                else//修改
                {

                    ModifyGoodsDetail(e.Row.Index);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid1_BeforeRowsDeleted_1(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!m_bInsertFlag)
            {
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(ultraGrid1.ActiveRow.Cells[0].Value.ToString(), 10);
                TrainingDB.Model.CK_PayInTab modelCK_PayInTabOld = m_bllCK_PayInTab.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

                if (modelCK_PayInTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    try
                    {

                        m_bllCK_PayInTab.Delete(nSelectFinRecvMoneyDetailTab_NewID);



                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

    

        public string SqlValueList(string sql)
        {
            DataSet ds = DbHelperSQL.Query(sql.ToString());
            string strTotal = ds.Tables[0].Rows[0]["Total"].ToString();

            return strTotal;
        }

        /// <summary>    
        /// 执行SQL查询语句，返回所有记录的DataTable集合。    
        /// </summary>    
        /// <param name="sql">SQL查询语句</param>    
        /// <returns></returns>    
        public DataTable SqlTable(string sql)
        {
            DataSet ds = DbHelperSQL.Query(sql.ToString());
            return ds.Tables[0];
        }


        private DataTable FindToDataTable(string where, PagerInfo pagerInfo)
        {
            WHC.Pager.WinControl.PagerHelper helper = new WHC.Pager.WinControl.PagerHelper(@"select 
CK_PayInTab.CK_PayInTabID
--,CK_SchoolTab.CK_SchoolTabName
,CK_StudentInfo.stdtName
,CK_PayWayTab.CK_PayWayName
,CK_BankAccountTab.bankName
,T_ACL_User.Name
,payInDate
,coalesce(xuefee.tuitionSum,0) tuitionSum
--,referPYHC
,bookFeeSum
,otherFeeSum
,sumFee 
,payInMemo
--,inTime
from CK_PayInTab
 join CK_SchoolTab on CK_SchoolTab.CK_SchoolTabid = CK_PayInTab.CK_SchoolTabid 
 join T_ACL_User on T_ACL_User.ID = CK_PayInTab.CK_OperUserID
 join CK_StudentInfo on CK_StudentInfo.CK_StudentInfoID = CK_PayInTab.CK_StudentInfoID
 join CK_PayWayTab on CK_PayWayTab.CK_PayWayTabID = CK_PayInTab.CK_PayWayTabID
 join CK_BankAccountTab on CK_BankAccountTab.CK_BankAccountTabID = CK_PayInTab.CK_BankAccountTabID
 left join (select CK_PayInTabID, sum(tuition) tuitionSum from CK_PayInDetailClassTab group by CK_PayInTabID) xuefee on xuefee.CK_PayInTabID = CK_PayInTab.CK_PayInTabID 
", "*", "CK_PayInTabID", pagerInfo.PageSize, pagerInfo.CurrenetPageIndex, true, where);
            string countSql = helper.GetPagingSql(WHC.Pager.WinControl.DatabaseType.SqlServer, true);
            string dataSql = helper.GetPagingSql(WHC.Pager.WinControl.DatabaseType.SqlServer, false);

            string value = SqlValueList(countSql);
            pagerInfo.RecordCount = Convert.ToInt32(value);//为了显示具体的信息，需要设置总记录数
            DataTable dt = SqlTable(dataSql);
            this.pager1.InitPageInfo(pagerInfo.RecordCount, pagerInfo.PageSize);
            return dt;
        }

        private void BindData(string strWhere)
        {
            this.ultraGrid1.DataSource = null;
            DataTable dt = FindToDataTable(strWhere, this.pager1.PagerInfo);

            DataSet dsTemp = m_bllCK_StudentInfo.GetList("");

            this.ultraDropDownDep.DataSource = dsTemp.Tables[0];
            this.ultraDropDownDep.DisplayMember = "stdtName";
            this.ultraDropDownDep.ValueMember = "CK_StudentInfoID";
            this.ultraDropDownDep.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownDep.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownDep.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownDep.DisplayLayout.Bands[0].Columns["stdtName"].Hidden = false;


            dsTemp = m_bllCK_PayWayTab.GetList("");

            this.ultraDropDownInfoFrom.DataSource = dsTemp.Tables[0];
            this.ultraDropDownInfoFrom.DisplayMember = "CK_PayWayName";
            this.ultraDropDownInfoFrom.ValueMember = "CK_PayWayTabID";
            this.ultraDropDownInfoFrom.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownInfoFrom.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownInfoFrom.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownInfoFrom.DisplayLayout.Bands[0].Columns["CK_PayWayName"].Hidden = false;

            dsTemp = m_bllCK_BankAccountTab.GetList("");

            this.ultraDropDownIntentionDegree.DataSource = dsTemp.Tables[0];
            this.ultraDropDownIntentionDegree.DisplayMember = "bankName";
            this.ultraDropDownIntentionDegree.ValueMember = "CK_BankAccountTabID";
            this.ultraDropDownIntentionDegree.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            for (int i = 0; i < this.ultraDropDownIntentionDegree.DisplayLayout.Bands[0].Columns.Count; i++)
                this.ultraDropDownIntentionDegree.DisplayLayout.Bands[0].Columns[i].Hidden = true;
            this.ultraDropDownIntentionDegree.DisplayLayout.Bands[0].Columns["bankName"].Hidden = false;

   


            //DataSet ds = m_bllCK_PayInTab.GetList(strWhere);
            foreach (string strTemp in m_dicColName2AliasName.Keys)
            {
                dt.Columns[strTemp].ColumnName = m_dicColName2AliasName[strTemp];

            }
            this.ultraGrid1.DataSource = dt;

            this.ultraGrid1.DisplayLayout.Bands[0].Columns[0].Hidden = true;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["学员姓名"].ValueList = this.ultraDropDownDep;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["付款方式"].ValueList = this.ultraDropDownInfoFrom;
            this.ultraGrid1.DisplayLayout.Bands[0].Columns["银行账户"].ValueList = this.ultraDropDownIntentionDegree;

      
        }

        void pager1_PageChanged(object sender, EventArgs e)
        {
            BindData(m_filter);
        }

        void pager1_ExportCurrent(object sender, EventArgs e)
        {
            MessageUtil.ShowTips("导出当前页");
        }

        void pager1_ExportAll(object sender, EventArgs e)
        {
            MessageUtil.ShowTips("导出所有");
        }      
  
        private void Frm_ClassRoom_Load_1(object sender, EventArgs e)
        {
            this.pager1.PageChanged += new WHC.Pager.WinControl.PageChangedEventHandler(pager1_PageChanged);
            this.pager1.ExportCurrent += new WHC.Pager.WinControl.ExportCurrentEventHandler(pager1_ExportCurrent);
            this.pager1.ExportAll += new WHC.Pager.WinControl.ExportAllEventHandler(pager1_ExportAll);

            ClassUltraGridCommonSet.InitUltraWinGridSetting(this.ultraGrid1);
            //m_dicColName2AliasName.Add("CK_SchoolTabName", "校区名称");
            m_dicColName2AliasName.Add("stdtName", "学员姓名");
            m_dicColName2AliasName.Add("CK_PayWayName", "付款方式");
            m_dicColName2AliasName.Add("bankName", "银行账户");
            m_dicColName2AliasName.Add("Name", "录入人");


            m_dicColName2AliasName.Add("payInDate", "付款日期");
            m_dicColName2AliasName.Add("tuitionSum", "学费");
            m_dicColName2AliasName.Add("bookFeeSum", "教材费");
            m_dicColName2AliasName.Add("otherFeeSum", "其他费");
            m_dicColName2AliasName.Add("sumFee", "合计");
            m_dicColName2AliasName.Add("payInMemo", "备注");
            
       
            foreach (string strTemp in m_dicColName2AliasName.Keys)
            {
                m_dicColAliasName2Name.Add(m_dicColName2AliasName[strTemp], strTemp);
            }
        }

        private void buttonShowAll_Click_1(object sender, EventArgs e)
        {
            m_filter = "";
            ShowAllRecordInDataGridView(m_filter);
        }
        private void InitColumnWidth()
        {

            foreach (string strTemp in m_dicColName2AliasName.Keys)
            {
                ultraGrid1.DisplayLayout.Bands[0].Columns[m_dicColName2AliasName[strTemp]].Width = Frm_Main.m_nDfuColWidth;

            }
            //ultraGrid1.DisplayLayout.Bands[0].Columns["时段"].Width = 80;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["星期设置"].Width = 90;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["缺省学时"].Width = 50;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["教师课时费标准"].Width = 60;
            //ultraGrid1.DisplayLayout.Bands[0].Columns["是否排除节假日"].Width = 60;


        }
        private void ultraGrid1_InitializeLayout_1(object sender, InitializeLayoutEventArgs e)
        {
            InitColumnWidth();
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
        }

        private void 增加一行ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (!m_bInsertFlag)
            {
                AddNewRow();
                //UltraGridRow row = this.ultraGrid1.DisplayLayout.Bands[0].AddNew();
            }
        }

        private void 删除一行ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            if (!Frm_Main.CheckPerm(this.Text, "删除"))
            {
                MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }

                if (ultraGrid1.ActiveRow != null)
                {

                    ultraGrid1.ActiveRow.Delete(false);
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ultraGrid2_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            InitColumnWidth();
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
            SummarySettings averageSummary =
e.Layout.Bands[0].Summaries.Add(
"实收学费",
SummaryType.Sum,
e.Layout.Bands[0].Columns["实收学费"]);
            averageSummary.SummaryPosition = SummaryPosition.UseSummaryPositionColumn;
            averageSummary.SummaryPositionColumn = averageSummary.SourceColumn;
            averageSummary.DisplayFormat = "{0:.##}";
            averageSummary.Band.Override.SummaryFooterCaptionVisible = DefaultableBoolean.False;
            averageSummary.Band.Override.BorderStyleSummaryFooter = UIElementBorderStyle.None;
            averageSummary.SummaryDisplayArea = SummaryDisplayAreas.TopFixed;
        }

        private void ShowAllMXRecordInDataGridView(string strWhere)
        {
            ultraGrid2.DataSource = null;
            DataSet ds = m_bllCK_PayInDetailClassTab.GetList(strWhere);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataSet dsTemp = m_bllCK_ClassTab.GetList("");

                this.ultraDropDownClass.DataSource = dsTemp.Tables[0];
                this.ultraDropDownClass.DisplayMember = "CK_className";
                this.ultraDropDownClass.ValueMember = "CK_ClassTabID";
                this.ultraDropDownClass.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                for (int i = 0; i < this.ultraDropDownClass.DisplayLayout.Bands[0].Columns.Count; i++)
                    this.ultraDropDownClass.DisplayLayout.Bands[0].Columns[i].Hidden = true;
                this.ultraDropDownClass.DisplayLayout.Bands[0].Columns["CK_className"].Hidden = false;



                ultraGrid2.DataSource = ds.Tables[0];
                this.ultraGrid2.DisplayLayout.Bands[0].Columns["班级"].ValueList = this.ultraDropDownClass;

                ultraGrid2.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid2.DisplayLayout.Bands[0].Columns[1].Hidden = true;
            }
            else
            {
                ultraGrid2.DataSource = ds.Tables[0];
                ultraGrid2.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid2.DisplayLayout.Bands[0].Columns[1].Hidden = true;
            }
        }
        private void ShowAllMXRecordInDataGridViewOtherFee(string strWhere)
        {
            ultraGrid3.DataSource = null;
            DataSet ds = m_bllCK_PayInDetailOtherTab.GetList(strWhere);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                DataSet dsTemp = m_bllCK_ClassTab.GetList("");

                this.ultraDropDownClass2.DataSource = dsTemp.Tables[0];
                this.ultraDropDownClass2.DisplayMember = "CK_className";
                this.ultraDropDownClass2.ValueMember = "CK_ClassTabID";
                this.ultraDropDownClass2.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                for (int i = 0; i < this.ultraDropDownClass2.DisplayLayout.Bands[0].Columns.Count; i++)
                    this.ultraDropDownClass2.DisplayLayout.Bands[0].Columns[i].Hidden = true;
                this.ultraDropDownClass2.DisplayLayout.Bands[0].Columns["CK_className"].Hidden = false;


                dsTemp = m_bllCK_OtherFeeItemTab.GetList("");

                this.ultraDropDownFeeItem.DataSource = dsTemp.Tables[0];
                this.ultraDropDownFeeItem.DisplayMember = "CK_OtherFeeItemName";
                this.ultraDropDownFeeItem.ValueMember = "CK_OtherFeeItemTabID";
                this.ultraDropDownFeeItem.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                for (int i = 0; i < this.ultraDropDownFeeItem.DisplayLayout.Bands[0].Columns.Count; i++)
                    this.ultraDropDownFeeItem.DisplayLayout.Bands[0].Columns[i].Hidden = true;
                this.ultraDropDownFeeItem.DisplayLayout.Bands[0].Columns["CK_OtherFeeItemName"].Hidden = false;

                ultraGrid3.DataSource = ds.Tables[0];
                this.ultraGrid3.DisplayLayout.Bands[0].Columns["班级名称"].ValueList = this.ultraDropDownClass2;
                this.ultraGrid3.DisplayLayout.Bands[0].Columns["收费项目"].ValueList = this.ultraDropDownFeeItem;

                ultraGrid3.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid3.DisplayLayout.Bands[0].Columns[1].Hidden = true;
            }
            else
            {
                ultraGrid3.DataSource = ds.Tables[0];
                ultraGrid3.DisplayLayout.Bands[0].Columns[0].Hidden = true;
                ultraGrid3.DisplayLayout.Bands[0].Columns[1].Hidden = true;
            }

        }
        private void ultraGrid2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    AddNewRowultraGrid2();
                    //UltraGridRow row = this.ultraGrid2.DisplayLayout.Bands[0].AddNew();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        string strTemp = this.ultraGrid2.ActiveCell.Column.Header.Caption;

                        int nColumnIndex = this.ultraGrid2.ActiveCell.Column.Index;
                        int nIndex = ultraGrid2.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid2.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        string strTemp = this.ultraGrid2.ActiveCell.Column.Header.Caption;

                        int nColumnIndex = this.ultraGrid2.ActiveCell.Column.Index;
                        int nIndex = ultraGrid2.ActiveRow.Index;
                        if (nIndex < ultraGrid2.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid2.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid2.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid2.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid2.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid2.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid2.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }

        private void ultraGrid2_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!m_bInsertFlag2)
            {
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(ultraGrid2.ActiveRow.Cells[0].Value.ToString(), 10);
                TrainingDB.Model.CK_PayInDetailClassTab modelCK_PayInDetailClassTabOld = m_bllCK_PayInDetailClassTab.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

                if (modelCK_PayInDetailClassTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    try
                    {
                        m_bllCK_PayInDetailClassTab.Delete(nSelectFinRecvMoneyDetailTab_NewID);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid2_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid2.Rows[e.Row.Index];

                if (m_bInsertFlag2)
                {
                    ultraGrid2.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;
                    if (!AddGoodsDetail2(e.Row.Index))
                        e.Cancel = true;
                }
                else//修改
                {

                    ModifyGoodsDetail2(e.Row.Index);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid2_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid2.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid2.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid2.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid2.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag2 = true;
            InitNewAddRowValue2(e.Row.Index);
        }

        private void ultraGrid2_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid2.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex2 = ultraGrid2.ActiveRow.Index;
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            if (!m_bInsertFlag2)
            {
                TrainingDB.Model.CK_PayInTab modelCK_PayInTab = m_bllCK_PayInTab.GetModel(m_nStudentReferID);
                if (modelCK_PayInTab == null)
                {
                    MessageBox.Show("收费记录没有选中，所以无法增加班级收费明细!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    AddNewRowultraGrid2();
                    //UltraGridRow row = this.ultraGrid2.DisplayLayout.Bands[0].AddNew();
                }
            }
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (!Frm_Main.CheckPerm(this.Text, "删除"))
            {
                MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            int nIndex = m_nDelIndex2;
            if (m_nDelIndex2 >= 0)
            {
                DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }

                if (ultraGrid2.ActiveRow != null)
                {

                    ultraGrid2.ActiveRow.Delete(false);
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void InitNewAddRowValue2(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid2.Rows[nIndex];
            TrainingDB.Model.CK_PayInTab modelCK_PayInTab = m_bllCK_PayInTab.GetModel(m_nStudentReferID);
            bool bFind = false;
            int nTempClassID = 1;
            if(modelCK_PayInTab != null)
            {
                TrainingDB.Model.CK_StudentClassInfo modelCK_StudentClassInfo = m_bllCK_StudentClassInfo.GetModel(modelCK_PayInTab.CK_StudentInfoID);
                if (modelCK_StudentClassInfo != null)
                {
                    nTempClassID = modelCK_StudentClassInfo.CK_ClassTabID;
                    bFind = true;
                }
            }
            //modelCK_StudentInfo.
            
            
            CurrentUGR.Cells["班级"].Value = nTempClassID;
  
            CurrentUGR.Cells["应收学费"].Value = 0;
            CurrentUGR.Cells["实收学费"].Value = 0;
            CurrentUGR.Cells["优惠学费"].Value = 0;
            CurrentUGR.Cells["学期长度"].Value = 0;
            CurrentUGR.Cells["计费日期"].Value = System.DateTime.Now.Date;
            CurrentUGR.Cells["到期日期"].Value = System.DateTime.Now.Date;
            CurrentUGR.Cells["备注"].Value = "";

            if (!bFind)
            {
                TrainingDB.Model.CK_StudentClassInfo modelCK_StudentClassInfo = new TrainingDB.Model.CK_StudentClassInfo();
                modelCK_StudentClassInfo.CK_ClassTabID = 1;
                modelCK_StudentClassInfo.CK_StudentInfoID = modelCK_PayInTab.CK_StudentInfoID;
                modelCK_StudentClassInfo.inClsDate = System.DateTime.Now.Date;
                modelCK_StudentClassInfo.state = "正常";
                m_bllCK_StudentClassInfo.Add(modelCK_StudentClassInfo);
            }

            if (m_bInsertFlag2)
            {
                ultraGrid2.Rows[nIndex].Cells[0].Activation = Activation.ActivateOnly;
                m_bInsertFlag2 = false;
                AddGoodsDetail2(nIndex);

            }

        }
        private bool AddGoodsDetail2(int nRowIndex)
        {
            //return false;
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid2.Rows[nRowIndex];

                TrainingDB.Model.CK_PayInDetailClassTab modelCK_PayInDetailClassTab = new TrainingDB.Model.CK_PayInDetailClassTab();

                modelCK_PayInDetailClassTab.CK_PayInTabID = m_nStudentReferID;
                modelCK_PayInDetailClassTab.CK_ClassTabID = Convert.ToInt32(CurrentUGR.Cells["班级"].Value.ToString());

                modelCK_PayInDetailClassTab.termQtty = Convert.ToDecimal(CurrentUGR.Cells["学期长度"].Value);
                modelCK_PayInDetailClassTab.shallPayIn = Convert.ToDecimal(CurrentUGR.Cells["应收学费"].Value);
                modelCK_PayInDetailClassTab.rebateFee = Convert.ToDecimal(CurrentUGR.Cells["优惠学费"].Value);
                modelCK_PayInDetailClassTab.tuition = Convert.ToDecimal(CurrentUGR.Cells["实收学费"].Value);
                modelCK_PayInDetailClassTab.acctBeginDate = (DateTime)CurrentUGR.Cells["计费日期"].Value;
                modelCK_PayInDetailClassTab.nextPayinDate = (DateTime)CurrentUGR.Cells["到期日期"].Value;
                modelCK_PayInDetailClassTab.detail1Memo = CurrentUGR.Cells["备注"].Value.ToString();


                int nId = m_bllCK_PayInDetailClassTab.Add(modelCK_PayInDetailClassTab);
                ShowAllMXRecordInDataGridView("CK_PayInDetailClassTabid = '" + nId.ToString() + "'");
                //          MessageBox.Show("添加教室成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private bool ModifyGoodsDetail2(int nRowIndex)
        {

            try
            {
                if (!Frm_Main.CheckPerm(this.Text, "修改"))
                {
                    MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid2.Rows[nRowIndex];
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                TrainingDB.Model.CK_PayInDetailClassTab modelCK_PayInDetailClassTab = m_bllCK_PayInDetailClassTab.GetModel(nSelectFinRecvMoneyDetailTab_NewID);
                
                modelCK_PayInDetailClassTab.CK_PayInTabID = m_nStudentReferID;
                //modelCK_PayInDetailClassTab.CK_ClassTabID = Convert.ToInt32(CurrentUGR.Cells["班级"].Value.ToString());

                modelCK_PayInDetailClassTab.termQtty = Convert.ToDecimal(CurrentUGR.Cells["学期长度"].Value);
                modelCK_PayInDetailClassTab.shallPayIn = Convert.ToDecimal(CurrentUGR.Cells["应收学费"].Value);
                modelCK_PayInDetailClassTab.rebateFee = Convert.ToDecimal(CurrentUGR.Cells["优惠学费"].Value);
                modelCK_PayInDetailClassTab.tuition = Convert.ToDecimal(CurrentUGR.Cells["实收学费"].Value);
                modelCK_PayInDetailClassTab.acctBeginDate = (DateTime)CurrentUGR.Cells["计费日期"].Value;
                modelCK_PayInDetailClassTab.nextPayinDate = (DateTime)CurrentUGR.Cells["到期日期"].Value;
                modelCK_PayInDetailClassTab.detail1Memo = CurrentUGR.Cells["备注"].Value.ToString();

                string strBGH = CurrentUGR.Cells["班级"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelCK_PayInDetailClassTab.CK_ClassTabID = Convert.ToInt32(CurrentUGR.Cells["班级"].Value.ToString(), 10);
                }
                else
                {
                    DataSet ds = m_bllCK_ClassTab.GetList("CK_ClassName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelCK_PayInDetailClassTab.CK_ClassTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["CK_ClassTabID"].ToString(), 10);
                    }
                }


                m_bllCK_PayInDetailClassTab.Update(modelCK_PayInDetailClassTab);

                // MessageBox.Show("修改教室成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.tabControl1.SelectedIndex)
            {
                case 0:
                    {
                        ShowAllMXRecordInDataGridView("CK_PayInTabID = '" + m_nStudentReferID + "'");
                        break;
                    }
                case 1:
                    {
                        ShowAllMXRecordInDataGridViewOtherFee("CK_PayInTabID = '" + m_nStudentReferID + "'");
                        break;
                    }
                default:
                    break;
            }
        }
        private void ultraGrid3_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            InitColumnWidth();
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);
            ClassUltraGridCommonSet.InitUltraWinGridRowNumSetting(e);
        }
        private void ultraGrid3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
            {

                if (!m_bInsertFlag)
                {
                    AddNewRowultraGrid3();
                    //UltraGridRow row = this.ultraGrid3.DisplayLayout.Bands[0].AddNew();
                    //  row.Cells[2] = DateTime.Parse("1900-01-01");
                }
            }
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        string strTemp = this.ultraGrid3.ActiveCell.Column.Header.Caption;

                        int nColumnIndex = this.ultraGrid3.ActiveCell.Column.Index;
                        int nIndex = ultraGrid3.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid3.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid3.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid3.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid3.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid3.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid3.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        string strTemp = this.ultraGrid3.ActiveCell.Column.Header.Caption;

                        int nColumnIndex = this.ultraGrid3.ActiveCell.Column.Index;
                        int nIndex = ultraGrid3.ActiveRow.Index;
                        if (nIndex < ultraGrid3.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid3.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid3.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid3.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid3.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid3.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid3.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid3.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid3.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid3.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid3.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid3.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid3.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }

        private void ultraGrid3_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;
            if (!m_bInsertFlag3)
            {
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(ultraGrid3.ActiveRow.Cells[0].Value.ToString(), 10);
                TrainingDB.Model.CK_PayInDetailOtherTab modelCK_PayInDetailOtherTabOld = m_bllCK_PayInDetailOtherTab.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

                if (modelCK_PayInDetailOtherTabOld == null)
                {
                    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    try
                    {
                        m_bllCK_PayInDetailOtherTab.Delete(nSelectFinRecvMoneyDetailTab_NewID);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        e.Cancel = true;
                    }
                }
                else
                {
                    e.Cancel = true;
                }
            }
            else
            {
                MessageBox.Show("插入未完成，不能删除记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid3_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid3.Rows[e.Row.Index];

                if (m_bInsertFlag3)
                {
                    ultraGrid3.Rows[e.Row.Index].Cells[2].Activation = Activation.ActivateOnly;
                    m_bInsertFlag = false;
                    if (!AddGoodsDetail3(e.Row.Index))
                        e.Cancel = true;
                }
                else//修改
                {

                    ModifyGoodsDetail3(e.Row.Index);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void ultraGrid3_AfterRowInsert(object sender, RowEventArgs e)
        {
            ultraGrid3.Rows[e.Row.Index].Cells[1].IgnoreRowColActivation = true;
            ultraGrid3.Rows[e.Row.Index].Cells[1].Activation = Activation.AllowEdit;
            ultraGrid3.Rows[e.Row.Index].Cells[1].Activate();
            ultraGrid3.PerformAction(UltraGridAction.EnterEditMode);
            m_bInsertFlag3 = true;
            InitNewAddRowValue3(e.Row.Index);
        }

        private void ultraGrid3_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid3.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex3 = ultraGrid3.ActiveRow.Index;
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            if (!m_bInsertFlag3)
            {
                TrainingDB.Model.CK_PayInTab modelCK_PayInTab = m_bllCK_PayInTab.GetModel(m_nStudentReferID);
                if (modelCK_PayInTab == null)
                {
                    MessageBox.Show("收费记录没有选中，所以无法增加班级收费明细!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    AddNewRowultraGrid3();
                    //UltraGridRow row = this.ultraGrid3.DisplayLayout.Bands[0].AddNew();
                }
            }
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            if (!Frm_Main.CheckPerm(this.Text, "删除"))
            {
                MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            int nIndex = m_nDelIndex3;
            if (m_nDelIndex3 >= 0)
            {
                DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }

                if (ultraGrid3.ActiveRow != null)
                {

                    ultraGrid3.ActiveRow.Delete(false);
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }


        private void InitNewAddRowValue3(int nIndex)
        {
            UltraGridRow CurrentUGR = ultraGrid3.Rows[nIndex];
         

            CurrentUGR.Cells["收费项目"].Value = 1;
            CurrentUGR.Cells["班级名称"].Value = 1;
            CurrentUGR.Cells["费用"].Value = 0;
            CurrentUGR.Cells["摘要"].Value = "";
            CurrentUGR.Cells["备注"].Value = "";

            if (m_bInsertFlag3)
            {
                ultraGrid3.Rows[nIndex].Cells[0].Activation = Activation.ActivateOnly;
                m_bInsertFlag3 = false;
                AddGoodsDetail3(nIndex);

            }

        }
        private bool AddGoodsDetail3(int nRowIndex)
        {
            //return false;
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid3.Rows[nRowIndex];

                TrainingDB.Model.CK_PayInDetailOtherTab modelCK_PayInDetailOtherTab = new TrainingDB.Model.CK_PayInDetailOtherTab();

                modelCK_PayInDetailOtherTab.CK_PayInTabID = m_nStudentReferID;
                modelCK_PayInDetailOtherTab.oObjId = CurrentUGR.Cells["班级名称"].Value.ToString();
                modelCK_PayInDetailOtherTab.CK_OtherFeeItemTabID = Convert.ToInt32(CurrentUGR.Cells["收费项目"].Value.ToString());

                modelCK_PayInDetailOtherTab.otherFee = Convert.ToDecimal(CurrentUGR.Cells["费用"].Value);
                modelCK_PayInDetailOtherTab.detail3Memo = CurrentUGR.Cells["备注"].Value.ToString();
                modelCK_PayInDetailOtherTab.digest = CurrentUGR.Cells["摘要"].Value.ToString();

                int nId = m_bllCK_PayInDetailOtherTab.Add(modelCK_PayInDetailOtherTab);
                ShowAllMXRecordInDataGridViewOtherFee("CK_PayInDetailOtherTabid = '" + nId.ToString() + "'");
                //          MessageBox.Show("添加教室成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private bool ModifyGoodsDetail3(int nRowIndex)
        {

            try
            {
                if (!Frm_Main.CheckPerm(this.Text, "修改"))
                {
                    MessageBox.Show("对不起您没有该权限!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid3.Rows[nRowIndex];
                int nSelectFinRecvMoneyDetailTab_NewID = Convert.ToInt32(CurrentUGR.Cells[0].Value.ToString(), 10);
                TrainingDB.Model.CK_PayInDetailOtherTab modelCK_PayInDetailOtherTab = m_bllCK_PayInDetailOtherTab.GetModel(nSelectFinRecvMoneyDetailTab_NewID);

                //modelCK_PayInDetailOtherTab.CK_PayInTabID = m_nStudentReferID;
                //modelCK_PayInDetailOtherTab.oObjId = CurrentUGR.Cells["班级名称"].Value.ToString();
                //modelCK_PayInDetailOtherTab.CK_OtherFeeItemTabID = Convert.ToInt32(CurrentUGR.Cells["收费项目"].Value.ToString());

                modelCK_PayInDetailOtherTab.otherFee = Convert.ToDecimal(CurrentUGR.Cells["费用"].Value);
                modelCK_PayInDetailOtherTab.detail3Memo = CurrentUGR.Cells["备注"].Value.ToString();
                modelCK_PayInDetailOtherTab.digest = CurrentUGR.Cells["摘要"].Value.ToString();


                string strBGH = CurrentUGR.Cells["班级名称"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelCK_PayInDetailOtherTab.oObjId = CurrentUGR.Cells["班级名称"].Value.ToString();
                }
                else
                {
                    DataSet ds = m_bllCK_ClassTab.GetList("CK_ClassName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelCK_PayInDetailOtherTab.oObjId = ds.Tables[0].Rows[0]["CK_ClassTabID"].ToString();
                    }
                }

                strBGH = CurrentUGR.Cells["收费项目"].Value.ToString();
                if (ClassInputValid.IsNum(strBGH))
                {
                    modelCK_PayInDetailOtherTab.CK_OtherFeeItemTabID = Convert.ToInt32(CurrentUGR.Cells["收费项目"].Value.ToString());
                }
                else
                {
                    DataSet ds = m_bllCK_OtherFeeItemTab.GetList("CK_OtherFeeItemName = '" + strBGH + "'");
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        modelCK_PayInDetailOtherTab.CK_OtherFeeItemTabID = Convert.ToInt32(ds.Tables[0].Rows[0]["CK_OtherFeeItemTabID"].ToString());
                    }
                }

                m_bllCK_PayInDetailOtherTab.Update(modelCK_PayInDetailOtherTab);

                // MessageBox.Show("修改教室成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid2_AfterCellUpdate(object sender, CellEventArgs e)
        {
          
            int nIndex = e.Cell.Row.Index;
            if (nIndex < 0)
                return;
            UltraGridRow CurrentUGR = ultraGrid2.Rows[nIndex];
             
            UltraGridRow CurrentUGR1 = ultraGrid1.Rows[m_nDelIndex];
            if (e.Cell.Value.ToString() == "")
                return;

            if (e.Cell.Column.Key == "班级")
            {
                DataSet ds = m_bllCK_ClassTab.GetList("CK_ClassTab.CK_ClassTabID = '" + CurrentUGR.Cells["班级"].Value + "'");

                CurrentUGR.Cells["收费标准"].Value = (Decimal)ds.Tables[0].Rows[0]["chargeStd"];
                CurrentUGR.Cells["收费模式"].Value = ds.Tables[0].Rows[0]["CK_RecvMoneyWayName"].ToString();
     
            }
            else if (e.Cell.Column.Key == "学期长度")
            {
                Decimal dOld = (Decimal)CurrentUGR.Cells["实收学费"].Value;
                CurrentUGR.Cells["应收学费"].Value = (Decimal)CurrentUGR.Cells["收费标准"].Value * (Decimal)CurrentUGR.Cells["学期长度"].Value;
                CurrentUGR.Cells["实收学费"].Value = (Decimal)CurrentUGR.Cells["应收学费"].Value - (Decimal)CurrentUGR.Cells["优惠学费"].Value;
                CurrentUGR1.Cells["学费"].Value = (Decimal)CurrentUGR1.Cells["学费"].Value - dOld + (Decimal)CurrentUGR.Cells["实收学费"].Value;

            }
            else if (e.Cell.Column.Key == "优惠学费")
            {
                Decimal dOld = (Decimal)CurrentUGR.Cells["实收学费"].Value;
                CurrentUGR.Cells["实收学费"].Value = (Decimal)CurrentUGR.Cells["应收学费"].Value - (Decimal)CurrentUGR.Cells["优惠学费"].Value;
                CurrentUGR1.Cells["学费"].Value = (Decimal)CurrentUGR1.Cells["学费"].Value - dOld + (Decimal)CurrentUGR.Cells["实收学费"].Value;
            }
            else if (e.Cell.Column.Key == "计费日期")
            {
                DateTime dtTemp = (DateTime)CurrentUGR.Cells["计费日期"].Value;
                string strTemp = CurrentUGR.Cells["收费模式"].Value.ToString();
                Decimal dTemp = (Decimal)CurrentUGR.Cells["学期长度"].Value;
                
                if(strTemp == "按天")
                {
                    CurrentUGR.Cells["到期日期"].Value = (DateTime)dtTemp.AddDays((double)dTemp).Date;
                }
                else if(strTemp == "按月")
                {
                    CurrentUGR.Cells["到期日期"].Value = dtTemp.AddMonths((int)dTemp).Date;
                }
                else if (strTemp == "按季度")
                {
                    CurrentUGR.Cells["到期日期"].Value = (DateTime)dtTemp.AddMonths(3 * (int)dTemp).Date;
                }
                else if (strTemp == "按半年")
                {
                    CurrentUGR.Cells["到期日期"].Value = (DateTime)dtTemp.AddMonths(6 * (int)dTemp).Date;
                }
                else if (strTemp == "按一年")
                {
                    CurrentUGR.Cells["到期日期"].Value = (DateTime)dtTemp.AddYears((int)dTemp).Date;
                }
            }
        }





      

    }
}
